package twentytwenty;

/**
 * @Author: Spider Man
 * @Description: 9.判断数字是否回文
 * @Date Created in 2020-09-21 8:11
 * @Modified By:
 */
public class IsPalindrome_9 {
    public static void main(String [] args){
        int x = 1003001;
        System.out.println(isPalindrome2(x));
    }

    public static boolean isPalindrome(int x) {
        String change = x + "";
        int length = change.length();
        boolean result = false;
        int left = 0;
        int right = length - 1;
        if (left == right){
            return true;
        }
        while (left < right){
            if (change.charAt(left) == change.charAt(right)){
                left++;
                right--;
                result = true;
            }else {
                result = false;
                break;
            }
        }
        return result;
    }
        public static boolean isPalindrome2(int x) {
            //边界判断
            if (x < 0) return false;
            int div = 1;
            //用于取出头部
            while (x / div >= 10) div *= 10;
            while (x > 0) {
                //取出头部
                int left = x / div;
                //取出尾部
                int right = x % 10;
                if (left != right) return false;
                x = (x % div) / 10;
                div /= 100;
            }
            return true;
        }
}
